package com.reggie.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.Employee;
import com.reggie.mapper.EmployeeMapper;
import com.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1. md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 2. 查询数据库，通过名称查询用户
        LambdaQueryWrapper<Employee> queryWapper = new LambdaQueryWrapper<>();
        queryWapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWapper);
        // 3. 判断是否查到
        if(emp == null) {
            return R.error("用户不存在");
        }
        // 4. 如果密码不一致
        if(!emp.getPassword().equals(password)) {
            return R.error("密码不一致");
        }
        // 5. 查看用户是否被禁用
        if(emp.getStatus() == 0) {
            return R.error("用户被禁用");
        }

        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        req.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 保存用户，我们用户名是唯一的，因此可能出现异常
        employeeService.save(employee);
        // 返回值
        return R.success("新增用户成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page = {}, pageSize = {}, name = {}", page, pageSize, name);
        // 构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 如果名字不为空，添加过滤条件
        queryWrapper.like(Strings.isNotBlank(name),Employee::getName, name);
        // 添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询，并保存到pageInfo当中去
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {
        log.info("update", employee.toString());
        employeeService.updateById(employee);
        return R.success("更新成功!");
    }

    @GetMapping("{id}")
    public R<Employee> queryById(@PathVariable Long id) {
        Employee emp = employeeService.getById(id);
        if(emp != null) {
            return R.success(emp);
        }
        return R.error("查询失败");
    }
}
